<?php

$private_key = 12555;
$modulo = 40141;

if(isset($_GET['domain']))
{
	$referer_domain = $_GET['domain'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}
elseif(isset($_POST['domains'])&&!empty($_POST['domains']))
{
	$referer_domain = $_POST['domains'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}
elseif(isset($_GET['host'])&&!empty($_GET['host']))
{
	$referer_domain = $_GET['host'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}
elseif(isset($_SERVER['HTTP_REFERER'])&&!empty($_SERVER['HTTP_REFERER']))
{
	$referer_domain = $_SERVER['HTTP_REFERER'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}else
{
	$referer_domain = '';
}

$license = '';
if(!empty($referer_domain))
{
  $domains = trim($referer_domain);
  $start = date("Y-m-d", time()-60*60*24);
  $end = '*'; // навсегда

  include('license.php');
  $d = $domains;
  
  $c = $b = '';
  for($i=0; $i<rand(30, 40); $i++)
  {
  	$c .= rand(0,9);
  	$b .= rand(0,9);
  }
  $license = encode_license(array($domains), $end, $c);

  
  $auto = 0;
  if(isset($_GET['host'])&&!empty($_GET['host']))
  {
	$auto = 1;
  }
}

if(isset($_GET['host'])&&!empty($_GET['host']))
{
	print $license;
	exit();
}


?>
<html>
<head>
<title>Лицензия Simpla</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
td{font-family:arial; font-size:14px;text-align:left;}
th{font-family:arial; font-size:14px;text-align:left;}
input.text{font-family:arial; font-size:18px;width:500px;}
input.money{font-family:arial; font-size:18px;width:100px;}
input.submit{font-family:arial; font-size:18px;}
textarea.license{width:500px; height:200px;}
a.new{font-family:arial; font-size:18px; color:green;}
</style>
</head>
<body>


<form method=post>
<table border=0 cellpadding=10 cellspacing=10>
  <tr>
    <td>
    	Домен:
    </td>
    <td>
    	<input class=text type=text name=domains value='<?php if($domains)print $domains;else print $_POST['domains'] ?>'>
    </td>
  </tr>
  <tr>
    <td>
    </td>
    <td>
    	<input class=submit type=submit value='Получить вечный ключ'>
    </td>
  </tr>
<?php
if($referer_domain)
{  
  print "<tr><td></td><td>Ваш вечный ключ:<br><br><textarea class=license>".trim($license)."</textarea></td></tr>";
}
?>
</table>
</post>

</body>
</html>
<?php




// RSA шифрование
function rsa_encrypt($data, $private_key, $modulo)
{
		
  for ($j=0x0; $j<strlen($data); $j++)
		{
			$b=ord($data[$j]);
			$result = 1;
			for($i=0x0; $i<$private_key; $i++)
			{
				$result = ($result*$b) % $modulo;
			}
			$encrypted[$j]= base_convert($result, 10, 36);
		}
  return $encrypted;
}


// RSA дешифрование
function rsa_decrypt($data, $public_key, $modulo)
{
	$result = 1;
	$max = count($data);
	for ($j=0x0; $j<$max; $j++)
	{
		$b=base_convert($data[$j],36,10);
		$result = 1;
		for($i=0x0; $i<$public_key; $i++)
		{
			$result = ($result*$b) % $modulo;
		}
		$decr .= chr($result);
	}		
	return $decr;
}



function rsa_get_public_key($p, $q)
{

	$n=$p*$q;
	$ph=($p-1)*($q-1);
	
	$e = max($p,$q)-2;
	
	while (!isMutuallyDistinct($e, $ph))
		$e-=2;
	return $e;
}


function rsa_get_private_key($p, $q)
{
	$n=$p*$q;
	$ph=($p-1)*($q-1);
	
	$e = rsa_get_public_key($p, $q);
	
	$u1=1;
	$u2=0x0;
	$u3=$ph;
	$r1=0x0;
	$r2=1;
	$r3=$e;
	
	while ($r3)
	{
		if(!$r3)
		 break;
		 
		$q=(int)(((int)$u3)/((int)$r3));
		$t1=($u1-$r1*$q);
		$t2=($u2-$r2*$q);
		$t3=($u3-$r3*$q);
		$u1=$r1;
		$u2=$r2;
		$u3=$r3;
		$r1=$t1;
		$r2=$t2;
		$r3=$t3;
	}
	return $d=$u2;
}


// Проверка на чисел на взаимную простоту
function isMutuallyDistinct($a, $b)
{
	while(($a!=0x0) && ($b!=0x0))
		if($a>=$b) $a=$a%$b;
			else $b=$b%$a;
	
	return (($a+$b)==1)?true:false;
}




function create_license($domain, $start, $end, $comment, $private_key, $modulo)
{
  $license = '';

  $text = $domain.';'.$start.';'.$end.';'.$comment;
  $text .= '#'.md5($text);
  
  $ed = rsa_encrypt($text,  $private_key, $modulo);
  

     foreach($ed as $s)
     {
       $license .= $s.' ';  
     }

  return $license;
}

function read_license($license, $public_key, $modulo)
{
  $ed = split(' ', $license);

  $decoded = rsa_decrypt($ed, $public_key, $modulo);
  
  $license = split('#', $decoded);
  
  $hash = $license[1];
  $data = $license[0];
  
  print $hash;
  if(trim($hash) != md5(trim($data)))
  	$data .= "//не подписана";
 
  return $data;
}




?>